﻿#ifndef ROBOR_H
#define ROBOR_H
#define BOOST_BIND_GLOBAL_PLACEHOLDERS

#if defined(GNUC)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored “- Wdeprecated - declarations”
#elif defined(_MSC_VER)
#pragma warning(disable : 4996)
#endif

#include <json/json.h>
#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/bind.hpp>
#include <boost/bind/bind.hpp>
#include <boost/algorithm/string.hpp>
#include <time.h>
#include <vector>
#include <string>
#include <iostream>
#include <memory>
#include <sstream>

using namespace boost::asio;
using namespace boost::placeholders;

unsigned int CRC32(void *pData, size_t iLen);

typedef struct cmdPara
{
    enum Type
    {
        MOVJ = 1,
        MOVL
    };
    double m_velocity;
    double m_acc;
    double m_dec;
    double m_pl;
    double m_position[8];
    Type cmdType;
    cmdPara()
    {
        m_velocity = 0;
        m_acc = 0;
        m_dec = 0;
        m_pl = 0;
        for (int i = 0; i < 8; i++)
        {
            m_position[i] = 0;
        }
        cmdType = MOVJ;
    }
} cmdPara;

class Robot
{

private:
    io_service io;
    ip::tcp::socket *socket_robot;    //TCP通信使用的Socket
    ip::tcp::endpoint robot_endpoint; //控制器地址,6000为一般操作端口，导入导出升级为5000
    bool is_connected;
    std::string ip;
public:
    Robot();
    ~Robot();
    void sendMessage(char cmd[2], std::string str_t); //向发送指令
    void sendJsonMsg(char cmd[2], Json::Value root);  //发送JSON格式
    Json::Value recieveMessage(char cmd[2]);          //接收数据

    /*回调函数*/
    void conn_handler(const std::error_code &ec, std::shared_ptr<ip::tcp::socket> sock); //建立tcp连接
    // void timer_handler(const std::error_code &ec, boost::asio::steady_timer *t);         //心跳检测

    /*机器人控制命令*/
    void connect_robot(std::string ip); //连接Robot, 执行一次

    void servo_power_on();

    void servo_power_off();

    void robot_movj(double *pos, int vel, int coord);

    void robot_movl(double *pos, int vel, int coord);

    void robot_movc(double *pos1, double *pos2, int vel, int coord);

    void robot_movca(double *pos1, double *pos2, int vel, int coord);

    int get_robot_running_state();

    void start_jogging(int axis, int direction);

    void stop_jogging(int axis);

    void set_dout(int port, int value);

    void set_aout(int port, double value);

    void get_dout(double *dout);

    void get_din(double *din);

    void get_aout(double *aout);

    void get_ain(double *ain);

    void get_current_position(double *pos,int coord);

    void set_jogging_speed(int speed);

    void continuous_motion_mode(int on); //0 close;1 open;

    void send_continuous_motion_queue(cmdPara cmd[], unsigned int size);
};

#endif
